/**
					Enhance Wars

	@author 	patarik
	@startdate	26.11.2010
	@version 	0
	@date 		26.11.2010	

 free mem from $2000 to $C000

*/

SAVMSC EQU  88

SDMCTL EQU	559	
DMACTL EQU	$D400 
        	
SDLSTL EQU	560
DLISTL EQU	$D402
COLOR1 EQU	709
COLOR2 EQU	710
COLPF1 EQU	$D017          	
NMIEN  EQU	$D40E
VVBLKD EQU	548
SETVBV EQU	$E45C
XITVBV EQU  $E462

//PMG
GRACTL EQU	$D01D 
PMBASE EQU	$D407
HPOSP0 EQU	$D000          	 
SIZEP0 EQU	$D008
PCOLR0 EQU	$02C0
COLPM0 EQU	$D012
GPRIOR EQU	$026F
PRIOR  EQU	$D10B


//INPUT
STICK0 EQU	$0278
PACTL  EQU	$D302

RTCLOK EQU 	20
//DLI
WSYNC  EQU  $D40A          	
VCOUNT EQU	$D40B
//CHBASE
CHBAS EQU	756

/**
	Application specific equ-s
*/
CURSOR_HEIGHT EQU 10
CURSOR_MIN_X  EQU 48+16   ; these are valid for narrow mode a our cursor
CURSOR_MIN_Y  EQU 16-1 ;-1 leading zero in player
CURSOR_MAX_X  EQU 208-16-8  ;-8 = cursor width
CURSOR_MAX_Y  EQU 112-16-CURSOR_HEIGHT+1 ;+1 ending zero in player
CURSOR_STEP	  EQU 8			; move step
CURSOR_DELAY  EQU 10
//ZP
pointer0 EQU $B0

//scr_mem1 EQU $C000 	
//scr_mem2 EQU scr_mem1+$1400
pm_base	 EQU $A000 //E800
player0_base EQU pm_base+512
//cursor_data EQU $B000

//charset
charset_gr0_base EQU $B000
	
	org $2000
interlace_switcher 	dta 0
cursor_x_pos		dta CURSOR_MIN_X
cursor_y_pos		dta CURSOR_MIN_Y
cursor_y_last_pos	dta CURSOR_MIN_Y
cursor_delay_counter dta CURSOR_DELAY 	
start
	
//colors	
	mva #0 COLOR2
	mva #15 COLOR1
	mva #4*16+8 PCOLR0 //red?
//charset
	mva >charset_gr0_base  CHBAS
//display list		
	mwa #scr_mem1 88 
	
	sei
	mva #0 SDMCTL					; turn off antic
	mwa #display_list_1 SDLSTL	; setting new display list

// VBL	
	//seting new VBI 2 stage routine
	ldy <vbl_stage_2
    ldx >vbl_stage_2
    LDA #7
    JSR SETVBV

	mva #32+1+8 SDMCTL	;DMA ON + narrow playfield mode + turn on PMG player DMA + double line player resolution
	cli 
	
// PMG
	mva #>pm_base PMBASE  //PMG data
	mva #0 SIZEP0 //double size
	mva #2 GRACTL //To turn on players 
	mva #1 GPRIOR //priority Player 0 - 3, playfield 0 - 3, BAK
	
	//draw cursor for the first time on initial position
	mva cursor_x_pos HPOSP0
	jsr draw_cursor
//reading joystick reset
	mva #4 PACTL
	
main_loop



	jmp main_loop

/**
	Subroutine 
	drawing PMG player 0 aka cursor
	
*/
draw_cursor
	pha
	tya
	pha
	
	//clear old position
	clc
	lda <player0_base
	adc cursor_y_last_pos
	sta pointer0
	lda >player0_base
	adc #0
	sta pointer0+1
	
	ldy #0
clear_cursor	
	mva #0 (pointer0),y+
	cpy #CURSOR_HEIGHT
	bne clear_cursor
	
	// where to draw
	clc
	lda <player0_base
	adc cursor_y_pos
	sta pointer0
	lda >player0_base
	adc #0
	sta pointer0+1
	
	ldy #0
fill_cursor	
	mva cursor_data,y (pointer0),y+
	cpy #CURSOR_HEIGHT
	bne fill_cursor

	pla
	tay
	pla
	
	rts


/** 
				VBL routine
*/

vbl_stage_2
// switching DList
	phr
	lda interlace_switcher
	jne switch_gr15
	mwa #display_list_1 SDLSTL
	//mwa #display_list_1 DLISTL
	mva #1 interlace_switcher
	jne vbl_end	
switch_gr15
	mwa #display_list_2 SDLSTL
	//mwa #display_list_2 DLISTL
	mva #0 interlace_switcher
vbl_end

// reading joystick, moving and drawing cursor 
	dec	cursor_delay_counter
	jne exit_reading_joy
	mva #CURSOR_DELAY cursor_delay_counter
	
	jcs exit_reading_joy
	cpb STICK0 #15			; no move?
	jeq exit_reading_joy
l_left
	lda STICK0
	and #%1100 		;left?
	cmp #%1000
	jne l_right
	cpb cursor_x_pos #CURSOR_MIN_X
	jeq l_right
	sec
	lda cursor_x_pos
	sbc #CURSOR_STEP
	sta cursor_x_pos
	sta HPOSP0
	jmp l_up
l_right
	lda STICK0
	and #%1100
	cmp #%0100		;right?
	jne l_up
	cpb cursor_x_pos #CURSOR_MAX_X
	jeq l_up
	clc
	lda cursor_x_pos
	adc #CURSOR_STEP
	sta cursor_x_pos
	sta HPOSP0
l_up
	lda STICK0 
	and #%0011			; up?
	cmp #%0010	
	jne l_down
	cpb cursor_y_pos  #CURSOR_MIN_Y
	jeq l_down
	sec
	lda cursor_y_pos
	sta cursor_y_last_pos
	sbc #CURSOR_STEP
	sta cursor_y_pos
	jsr draw_cursor
	jmp exit_reading_joy
l_down
	lda STICK0 
	and #%0011
	cmp #%0001			;down?	 
	jne exit_reading_joy
	cpb cursor_y_pos  #CURSOR_MAX_Y
	jeq exit_reading_joy
	clc
	lda cursor_y_pos
	sta cursor_y_last_pos
	adc #CURSOR_STEP
	sta cursor_y_pos
	jsr draw_cursor	
exit_reading_joy
	plr
	jmp XITVBV

/**
*	We are changing between gr.0 a gr.12 
*	antic 2 antic 4
*/
display_list_1
	:+3  dta $70
		 dta $42, a(scr_mem1) //LMS
	:+9  dta $04,$02
		 dta $04		
		 dta $41, a(display_list_1) //jump and wait

display_list_2
	:+3  dta $70
		 dta $44, a(scr_mem1) //LMS
	:+9  dta $02,$04
		 dta $02		
		 dta $41, a(display_list_2) //jump and wait
	

scr_mem1
	dta 0,1,d 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPP'
	dta 2,3,d 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPP'
	dta d 'PPPP',4,5, d'PPPPPPPPPPPPPPPPPPPPPPPPPP'
	dta d 'PPPP',6,7, d'PPPPPPPPPPPPPPPPPPPPPPPPPP'
:14	dta d 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP'
	dta d 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP'
	dta d 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP'


//PMG player0 data	aka cursor
cursor_data equ *			;
		 dta 0				;		##      ##
	   	 dta 195			;		#        #
	:+1	 dta 129			;		
	:+4  dta 0				;
	:+1  dta 129			;	    #		 #
		 dta 195			;		##   	##
		 dta 0				;		

	org charset_gr0_base
	ins 'gr8charset.fnt'

	run start

	


	
	
		 